<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row>
        <!-- 添加商品按钮 -->
        <el-col :span="1">
          <el-button @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 列表展示 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <template slot="isOk" slot-scope="scope">
          <i v-if="scope.row.cat_deleted" class="el-icon-error" style="color:red"></i>
          <i v-else class="el-icon-success" style="color:lightgreen"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag size="mini" v-else type="success">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope>
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </tree-table>
      <Page
        @handleSizeChange="handleSizeChange($event)"
        @handleCurrentChange="handleCurrentChange($event)"
        :total="total"
      ></Page>
      <!-- 分页 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="quiryList.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="quiryList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>-->
    </el-card>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="30%" @close="addCateDialogClose">
      <!-- 表单 -->
      <el-form :model="cateForm" :rules="catarules" ref="cateruleForm" label-width="100px">
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类" prop="cat_name">
          <el-cascader
            expand-trigger="hover"
            :props="cascaderProps"
            v-model="selectKeys"
            :options="parentCateList"
            @change="parentsCateChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Page from '../Pagin/Pagination.vue'
import { async } from 'q'
export default {
  components: { Page },
  
  data() {
    return {
      quiryList: {
        type: '',
        // 第几页
        pagenum: 1,
        // 多少条
        pagesize: 5
      },
      // 获取商品分类列表
      cateList: [],
      // 总数
      total: 0,
      // 表单设置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 表示当前列为模板列
          type: 'template',
          // 表示当前列的模板名称
          template: 'isOk'
        },
        {
          label: '排序',
          // 表示当前列为模板列
          type: 'template',
          // 表示当前列的模板名称
          template: 'order'
        },
        {
          label: '操作',
          // 表示当前列为模板列
          type: 'template',
          // 表示当前列的模板名称
          template: 'opt'
        }
      ],
      addDialogVisible: false,
      cateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      catarules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //  选中的父级分类的id数组
      selectKeys: [],

      // 父级分类列表
      parentCateList: [],
      // 图片上传
      header:{
        Authorization: window.sessionStorage.getItem('token')
      }
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('/categories', {
        params: this.quiryList
      })
    //   console.log(res)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听pagesize改变
    handleSizeChange(newsize) {
      this.quiryList.pagesize = newsize
      this.getCateList()
    },
    handleCurrentChange(newpage) {
      this.quiryList.pagenum = newpage
      this.getCateList()
    },
    // 添加分类
    addCate() {
      this.getParentCateList()
      this.addDialogVisible = true
    },
    // 获取分类列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('/categories', {
        params: { type: 2 }
      })
    //   console.log(res)
      this.parentCateList = res.data
    },
    parentsCateChange() {
    //   console.log(this.selectKeys)
      if(this.selectKeys.length > 0) {
        //   获取最后一个父级id
          this.cateForm.cat_pid = this.selectKeys[this.selectKeys.length -1]
        //   获取父级等级
          this.cateForm.cat_level = this.selectKeys.length -1
      }
      else {
          this.cateForm.cat_pid = 0
           this.cateForm.cat_level = 0
      }
    },
    // 添加商品分类
    add () {
        console.log(this.cateForm);
        this.$refs.cateruleForm.validate(async valid => {
            console.log(valid);
            if(!valid) {
                this.$message.error('添加失败')
            } else {
                const {data:res} = await this.$http.post('/categories',this.cateForm)
                console.log(res)
                if(res.meta.status !== 201) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                this.getCateList()
                this.addCateDialogClose = true
            }
            
        })
        
    },
    // 关闭dialog
    addCateDialogClose(){
        // alert(1)
        this.$refs.cateruleForm.resetFields()
        this.selectKeys = []
        this.cateForm.cat_level = 0
        this.cateForm.cat_pid = 0

    }
  },
  created() {
    this.getCateList()
  }
}
</script>
<style lang='css' scoped>
.el-cascader {
  width: 100%;
}

</style>